//
//  BCSwitchView.m
//  NiuBanJin
//
//  Created by Billy on 15/10/14.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "BCSwitchView.h"

@interface BCSwitchView ();

@property (nonatomic, strong) UIView *view_scroll; //底部滚动view

@end

@implementation BCSwitchView

- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *) items scrollColor:(UIColor *) color {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.color_scroll = color;
        self.arry_items = items;
        [self setBackgroundColor:[UsefulUtil colorWithHexString:@"#ffffff"]];
        [self createView];
    }
    
    return  self;
}

#pragma mark - add the buttons by the number of items

- (void) createView {
    
    for (int i = 0; i < self.arry_items.count ; i ++) {
        
        CGRect frame = CGRectMake(SCREEN_WIDTH/self.arry_items.count * i,0 ,SCREEN_WIDTH/self.arry_items.count, self.bounds.size.height);
        UIFont *font = [UIFont systemFontOfSize:15];
        
        UIButton *tmp = [self buttonCreatonWithTitle:self.arry_items[i] frame:frame fontName:font titleColor:[UsefulUtil colorWithHexString:@"#434343"]];
        
        [tmp setTag:i+100];
        [self addSubview:tmp];
    }
    
    //底部红线
    UIView *view_tmp = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 2.0, SCREEN_WIDTH/self.arry_items.count, 2.0)] ;
    self.view_scroll = view_tmp;
    [view_tmp setTag:200];
    [view_tmp setBackgroundColor:self.color_scroll];
    [self addSubview:view_tmp];
}

- (UIButton *) buttonCreatonWithTitle:(NSString *) title frame:(CGRect ) frame  fontName:(UIFont *) fontName titleColor:( UIColor *) titleColor {
    
    UIButton *tmp = [[UIButton alloc] initWithFrame:frame];
    [tmp setTitle:title forState:UIControlStateNormal];
    [tmp.titleLabel setFont:fontName];
    [tmp setTitleColor:titleColor forState:UIControlStateNormal];
    
    [tmp addTarget:self action:@selector(buttonSelectedInCustomView:) forControlEvents:UIControlEventTouchUpInside];
    return tmp;
}

- (void) buttonSelected:(UIButton *) button {
    
    NSLog(@"parent view clicked");
}

- (void) buttonSelectedInCustomView: (UIButton *) button {

    [self.delegate buttonSelected:button];
}

@end
